package com.jy.rhin.infra.repo.node;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jy.common.UIDWorker;
import com.jy.rhin.domain.document.node.NodeAuthenticationPermissionRelationDao;
import com.jy.rhin.infra.repo.node.mapper.NodeAuthenticationPermissionRelationMapper;
import com.jy.rhin.infra.repo.node.po.NodeAuthenticationPermissionRelationPo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class NodeAuthenticationPermissionRelationDaoImpl implements NodeAuthenticationPermissionRelationDao {

    @Resource
    UIDWorker uidWorker;
    @Resource
    private NodeAuthenticationPermissionRelationMapper nodeAuthenticationPermissionRelationMapper;


    @Override
    public List<NodeAuthenticationPermissionRelationPo> getPermissionListByNodeId(Long nodeId) {
        QueryWrapper<NodeAuthenticationPermissionRelationPo> wrapper = new QueryWrapper<>();
        wrapper.eq(nodeId!=null,"node_id",nodeId);
        return nodeAuthenticationPermissionRelationMapper.selectList(wrapper);
    }

    @Override
    public Boolean save(List<NodeAuthenticationPermissionRelationPo> nodeAuthenticationPermissionRelationPoList) {
        UpdateWrapper<NodeAuthenticationPermissionRelationPo> wrapper = new UpdateWrapper<>();
        wrapper.eq("node_id",nodeAuthenticationPermissionRelationPoList.get(0).getNodeId());
        nodeAuthenticationPermissionRelationMapper.delete(wrapper);
        nodeAuthenticationPermissionRelationPoList.forEach(v->{
            v.setId(uidWorker.nextId());
            if(v.getPermissionId()!=null){
                nodeAuthenticationPermissionRelationMapper.insert(v);
            }
        });
        return true;
    }

	@Override
	public int getPermissionByPkAndCode(String publicKey, String action) {
		return nodeAuthenticationPermissionRelationMapper.getByPkAndCode(publicKey, action);
	}
}
